<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Delete a photo. Both in database and filesystem.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Modifies the returning way.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

require_once INSTALLDIR . '/lib/slh_action.php';
require_once INSTALLDIR . '/lib/slh_util.php';

require_once INSTALLDIR . '/classes/Photo.php';

class DeletephotoAction extends Action
{
	/**
	 * The object in the 'photo'.
	 */
	var $photo;
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_PHOTOSET)) {
			return false;
		}

		parent::prepare($args);
		
		
		// Get the photo_id.
		$photo_id = $this->getTrimmedArg('gphoto_id');
		
		if ( !is_numeric($photo_id)) {
			$this->clientError('没有找到这个相片呃.');
			return false;
		}
		
		$this->photo = Photo::staticGet('id', $photo_id);
		if (empty($this->photo)) {
			$this->clientError('找不到这张照片呃.');
		}

		return true;
	}
	
	function handle($args)
	{
		// Delete the photo directly.
		$photoset_id = $this->photo->photoset_id;
		
		$this->photo->delete();
		
		$url = slh_get_returnto();
		if ( !is_null($url)) {
			// Clear it up.
			slh_clear_returnto();
			slh_redirect($url, 303);
		} else {
			slh_redirect(slh_action2url('editphotoset', array('gphotoset_id' => $photoset_id)), 303);
		}
	}
}

?>